/*
 * NewPasswordDialog.java
 *
 * Created on May 21, 2012, 4:17:37 PM
 */
package org.passwordstore.gui;

import javax.swing.JOptionPane;
import org.passwordstore.Resource;
import org.passwordstore.util.Password;

/**
 *
 * @author thotheolh
 */
public class NewPasswordDialog extends javax.swing.JDialog {

    /** Creates new form NewPasswordDialog */
    public NewPasswordDialog() {
        initComponents();
        setSize(400, 190);
        pack();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bottomPane = new javax.swing.JPanel();
        btnPane = new javax.swing.JPanel();
        clearBtn = new javax.swing.JButton();
        submitBtn = new javax.swing.JButton();
        newPasswordPane = new javax.swing.JPanel();
        usrLbl = new javax.swing.JLabel();
        pwdLbl = new javax.swing.JLabel();
        rqLbl = new javax.swing.JLabel();
        pwdTf = new javax.swing.JTextField();
        userTf = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        rqTA = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bottomPane.setLayout(new java.awt.BorderLayout());

        clearBtn.setText("Clear");
        clearBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearBtnActionPerformed(evt);
            }
        });
        btnPane.add(clearBtn);

        submitBtn.setText("Submit");
        submitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitBtnActionPerformed(evt);
            }
        });
        btnPane.add(submitBtn);

        bottomPane.add(btnPane, java.awt.BorderLayout.EAST);

        getContentPane().add(bottomPane, java.awt.BorderLayout.PAGE_END);

        newPasswordPane.setMinimumSize(new java.awt.Dimension(137, 33));

        usrLbl.setText("Username: ");

        pwdLbl.setText("Password: ");

        rqLbl.setText("Required Place: ");

        pwdTf.setColumns(20);

        userTf.setColumns(20);

        rqTA.setColumns(20);
        rqTA.setLineWrap(true);
        rqTA.setRows(5);
        rqTA.setWrapStyleWord(true);
        jScrollPane2.setViewportView(rqTA);

        javax.swing.GroupLayout newPasswordPaneLayout = new javax.swing.GroupLayout(newPasswordPane);
        newPasswordPane.setLayout(newPasswordPaneLayout);
        newPasswordPaneLayout.setHorizontalGroup(
            newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(newPasswordPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rqLbl)
                    .addComponent(pwdLbl)
                    .addComponent(usrLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(pwdTf)
                    .addComponent(userTf))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        newPasswordPaneLayout.setVerticalGroup(
            newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(newPasswordPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usrLbl)
                    .addComponent(userTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdLbl)
                    .addComponent(pwdTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(newPasswordPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rqLbl)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(newPasswordPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {
        clear();
    }

    private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitBtnActionPerformed
        // TODO add your handling code here:
        Password tempPass = new Password(userTf.getText(), pwdTf.getText(), rqTA.getText());
        if (!Resource.getInstance().getSql().isPasswordExist(tempPass)) {
            if (Resource.getInstance().getSql().addPassword(tempPass)) {
                JOptionPane.showMessageDialog(this, "Password successfully added.", "Password added", JOptionPane.INFORMATION_MESSAGE);
                Resource.getInstance().refreshTables();
            } else {
                JOptionPane.showMessageDialog(this, "Password failed to be added.", "Password add failed", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Password already exists.\nFailed to be added.", "Password add failed", JOptionPane.WARNING_MESSAGE);
        }
        clear();
    }//GEN-LAST:event_submitBtnActionPerformed

    private void clear() {
        userTf.setText("");
        pwdTf.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPane;
    private javax.swing.JPanel btnPane;
    private javax.swing.JButton clearBtn;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel newPasswordPane;
    private javax.swing.JLabel pwdLbl;
    private javax.swing.JTextField pwdTf;
    private javax.swing.JLabel rqLbl;
    private javax.swing.JTextArea rqTA;
    private javax.swing.JButton submitBtn;
    private javax.swing.JTextField userTf;
    private javax.swing.JLabel usrLbl;
    // End of variables declaration//GEN-END:variables
}
